//
//  FSWForgetPwdCodeController.m
//  voicetip_ios
//
//  Created by 伏圣文 on 2020/2/15.
//  Copyright © 2020 fusw. All rights reserved.
//

#import "FSWForgetPwdCodeController.h"
#import "FSWForgetPwdEditController.h"
#import "UIButton+Gradient.h"
@interface FSWForgetPwdCodeController ()
@property (weak, nonatomic) IBOutlet UITextField *mobileFieldText;
@property (weak, nonatomic) IBOutlet UILabel *mobileLabel;
@property (weak, nonatomic) IBOutlet UILabel *codeLabel;
@property (weak, nonatomic) IBOutlet UITextField *sendCodeFieldText;

@end

@implementation FSWForgetPwdCodeController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"忘记密码";
    [self.sendButton setBackgroundColor:XCMainColor];
    //    [self.nextButton setBackgroundColor:XCMainColor];
    [self.nextButton gradientButtonWithSize:CGSizeMake(self.nextButton.xc_width, self.nextButton.xc_height) colorArray:@[(id)FSWNavEndColor,(id)FSWNavStartColor] percentageArray:@[@(0.0),@(1)] gradientType:GradientFromLeftToRight];

    @weakify(self)
    [[self.mobileFieldText rac_textSignal] subscribeNext:^(NSString* x) {
        @strongify(self)
        self.mobileFieldText.text = x.length > 11 ? [x substringToIndex:11]: x;
        if (11 == self.mobileFieldText.text.length && [FSWHelperTool isValidTelNum:x]) {
            self.sendButton.enabled = YES;
        }else{
            self.sendButton.enabled = NO;
        }
    }];
    
    [[self.mobileFieldText rac_textSignal] subscribeNext:^(NSString* x) {
        @strongify(self)
        if (self.mobileFieldText.text.length > 0) {
            self.mobileLabel.hidden = YES;
        }else{
            self.mobileLabel.hidden = NO;
        }
    }];
    
    [[self.sendCodeFieldText rac_textSignal] subscribeNext:^(NSString* x) {
        @strongify(self)
        if (self.sendCodeFieldText.text.length > 0) {
            self.codeLabel.hidden = YES;
        }else{
            self.codeLabel.hidden = NO;
        }
    }];
    
    [[RACSignal combineLatest:@[self.mobileFieldText.rac_textSignal,
                                self.sendCodeFieldText.rac_textSignal]
                       reduce:^(NSString* phone, NSString* code){
        return @(phone.length == 11 && code.length > 0);
    }]
     subscribeNext:^(id x) {
        if ([x boolValue]) {
            self.nextButton.enabled = YES;
            self.nextButton.backgroundColor = XCMainColor;
        }else{
            self.nextButton.enabled = NO;
            self.nextButton.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
        }
    }];
    
    
    
}
- (IBAction)nextButton:(id)sender {
    
    FSWForgetPwdEditController *vc = (FSWForgetPwdEditController *)[self instantiateViewControllerWithStoryboardName:@"Login" identifier:@"FSWForgetPwdEditController"];
    vc.mobile = self.mobileFieldText.text;
    vc.code = self.sendCodeFieldText.text;
    [self pushViewController:vc];
}
- (IBAction)sendCodeButton:(id)sender {
    @weakify(self);
    NSDictionary *dict = @{@"mobile": self.mobileFieldText.text};
    [LoginNetRequest sendCodeDataRequest:dict andBlock:^(id responseObject) {
        @strongify(self);
        self.sendButton.enabled = NO;

        [self.sendButton buttonCode:60 andBlock:^{
            @strongify(self);
            self.sendButton.enabled = YES;
        }];
    } andFailBlock:^(NSString *error) {
//        @strongify(self);
         [FSWToast showWithText:@"发送验证码失败"];
    }];
}

@end
